package com.gzx.plugin.tjzy.modular.phone.filter.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.gzx.plugin.tjzy.modular.phone.filter.entity.GzxPhoneFilter;
import com.gzx.plugin.tjzy.modular.phone.filter.entity.bo.GzxPhoneFilterAddBo;
import com.gzx.plugin.tjzy.modular.phone.filter.entity.bo.GzxPhoneFilterQueryBo;
import com.gzx.plugin.tjzy.modular.phone.filter.entity.vo.GzxPhoneFilterVo;
import com.gzx.plugin.tjzy.modular.phone.filter.mapper.GzxPhoneFilterMapper;
import com.gzx.plugin.tjzy.modular.phone.filter.service.GzxPhoneFilterService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.util.CommonCryptogramUtil;
import vip.xiaonuo.common.util.CommonExcelUtil;
import vip.xiaonuo.common.util.MapstructUtils;

import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/3/4 11:57
 */
@Service
@DS("tjzy")
@RequiredArgsConstructor
public class GzxPhoneFilterServiceImpl implements GzxPhoneFilterService {

    private final GzxPhoneFilterMapper phoneFilterMapper;

    /**
     * 分页查询
     *
     * @param filterPhoneQueryBo
     * @return
     */
    @Override
    public Page<GzxPhoneFilterVo> pageList(GzxPhoneFilterQueryBo filterPhoneQueryBo) {
        LambdaQueryWrapper<GzxPhoneFilter> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(filterPhoneQueryBo.getPhoneNumber())) {
            wrapper.eq(GzxPhoneFilter::getPhoneNumber, CommonCryptogramUtil.doSm1AesEncrypt(filterPhoneQueryBo.getPhoneNumber()));
        }
        if (StringUtils.isNotBlank(filterPhoneQueryBo.getName())) {
            wrapper.like(GzxPhoneFilter::getName, filterPhoneQueryBo.getName());
        }
        if (StringUtils.isNotBlank(filterPhoneQueryBo.getIdCard())) {
            wrapper.eq(GzxPhoneFilter::getIdCard, CommonCryptogramUtil.doSm1AesEncrypt(filterPhoneQueryBo.getIdCard()));
        }
        if (StringUtils.isNotBlank(filterPhoneQueryBo.getProjectCode())) {
            wrapper.eq(GzxPhoneFilter::getProjectCode, filterPhoneQueryBo.getProjectCode());
        }
        return phoneFilterMapper.selectVoPage(new Page<>(filterPhoneQueryBo.getCurrent(), filterPhoneQueryBo.getSize()),
                wrapper.orderByDesc(GzxPhoneFilter::getId));
    }

    /**
     * Excel导入
     *
     * @param projectCode
     * @param file
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(String projectCode, MultipartFile file) {
        List<GzxPhoneFilterVo> filterPhoneVoList = CommonExcelUtil.importExcel(file, GzxPhoneFilterVo.class);
        if (CollectionUtil.isNotEmpty(filterPhoneVoList)) {
            List<GzxPhoneFilter> filterPhoneList = MapstructUtils.convert(filterPhoneVoList.stream().filter(s -> StringUtils.isNotEmpty(s.getPhoneNumber())).toList(), GzxPhoneFilter.class);
            for (GzxPhoneFilter filterPhone : filterPhoneList) {
                if (StringUtils.isNotBlank(filterPhone.getPhoneNumber())) {
                    filterPhone.setPhoneNumber(CommonCryptogramUtil.doSm1AesEncrypt(filterPhone.getPhoneNumber()));
                }
                if (StringUtils.isNotBlank(filterPhone.getIdCard())) {
                    filterPhone.setIdCard(CommonCryptogramUtil.doSm1AesEncrypt(filterPhone.getIdCard()));
                }
            }
            List<List<GzxPhoneFilter>> batchList = Lists.partition(filterPhoneList, 350);
            batchList.forEach(batch -> phoneFilterMapper.insertBatchFilterPhone(projectCode, batch));
        }
    }

    /**
     * 新增或者修改
     *
     * @param filterPhoneAddBo
     */
    @Override
    public void add(GzxPhoneFilterAddBo filterPhoneAddBo) {
        phoneFilterMapper.insert(MapstructUtils.convert(filterPhoneAddBo, GzxPhoneFilter.class));
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        phoneFilterMapper.deleteById(id);
    }

    @Override
    public List<GzxPhoneFilter> selectList(LambdaQueryWrapper<GzxPhoneFilter> queryWrapper) {
        return phoneFilterMapper.selectList(queryWrapper);
    }
}
